<?php
namespace app\api\model;
use \think\Db;
use \think\Model;
/**
 * 收货地址
 */
class UserGoods extends Model
{
	//获取收货地址列表
    public static function lists($user_id=null,$page=1,$length=4) {
		$where = array();
		if (is_null($user_id)) {
            return $where;
        }else{
            $where['user_id'] = $user_id;
        }
		
        $data = Db::name('goods')
		->field('goods_id, user_name, user_tel, user_phone, province_id, city_id, country_id, address, post_code,is_default')
		->where($where)
		->order('goods_id')
		->limit($page,$length)
		->select()
		;
        $Cache = array();
        foreach ($data as $k=>$v) {
            $Cache[$k] = $v;
        }
        return $Cache;
    }
	
	//添加地址
    public static function addressAdd($data) {
       $where = array();
		if (is_null($data['user_id']) || empty($data['user_id'])) {
            return $where;
        }
		$nums = Db::name('goods')
		->insert($data)
		;
		if($nums == 0){
			return false;
		}else{
			return $nums;
		}
    }
	//修改地址
    public static function addressUpdate($data) {
        $where = array();
		if (is_null($data['goods_id']) || empty($data['goods_id'])) {
            return $where;
        }else{
            $where['goods_id'] = $data['goods_id'];
        }
		$nums = Db::name('goods')
		->where($where)
		->update($data)
		;
		if($nums == 0){
			return false;
		}else{
			return $nums;
		}
    }
	//删除地址
    public static function addressDelete($goods_id=null) {
        $where = array();
		if (is_null($goods_id)) {
            return $where;
        }else{
            $where['goods_id'] = $goods_id;
        }
		$nums = Db::name('goods')
		->where($where)
		->delete();
		;
		if($nums == 0){
			return false;
		}else{
			return $nums;
		}
    }
	//设置默认收货地址
    public static function setDefaultAddress($goods_id) {
        $where = array();
		if (is_null($goods_id)) {
            return $where;
        }else{
            $where['goods_id'] = $goods_id;
        }
		$nums = Db::name('goods')
		->where($where)
		->setField(['is_default' => 1])
		;
		if($nums == 0){
			return false;
		}else{
			return $nums;
		}
    }
	
	//收货地址详情
    public static function addressInfo($goods_id=null) {
        $where = array();
		if (is_null($goods_id)) {
            return $where;
        }else{
            $where['goods_id'] = $goods_id;
        }
        $data = Db::name('goods')
		->field('goods_id, user_name, user_tel, user_phone, province_id, city_id, country_id, address, post_code')
		->where($where)
		->find()
		;
        $Cache = array();
        foreach ($data as $k=>$v) {
            $Cache[$k] = $v;
        }
        return $Cache;
    }


}